package cn.wolfcode.trip.admin.controller;

import cn.wolfcode.trip.base.domain.Product;
import cn.wolfcode.trip.base.domain.ProductContent;
import cn.wolfcode.trip.base.query.ProductQuery;
import cn.wolfcode.trip.base.service.IProductBrandService;
import cn.wolfcode.trip.base.service.IProductCatalogService;
import cn.wolfcode.trip.base.service.IProductService;
import cn.wolfcode.trip.base.util.JsonResult;
import cn.wolfcode.trip.base.util.UploadUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.multipart.MultipartFile;

/**
 * 商品资源后台控制器
 */
@Controller
@RequestMapping("product")
public class ProductController {

    @Autowired
    private IProductService productService;

    @Autowired
    private IProductCatalogService productCatalogService;

    @Autowired
    private IProductBrandService productBrandService;

    /**
     * 商品页面
     * @param qo
     * @param model
     * @return
     */
    @RequestMapping("list")
    public String list(@ModelAttribute("qo") ProductQuery qo, Model model){
        //查询所有目录
        model.addAttribute("catalogs",productCatalogService.listAll());
        //查询所有品牌
        model.addAttribute("brands",productBrandService.listAll());
        model.addAttribute("pageInfo",productService.query(qo));
        return "product/list";
    }

    /**
     * 新增/修改
     * @param product
     * @param file
     * @return
     */
    @RequestMapping("saveOrUpdate")
    @ResponseBody
    public JsonResult saveOrUpdate(Product product, MultipartFile file){
        if(file!=null&&file.getSize()>0){
            String url = UploadUtil.uploadQiniu(file);
            product.setCoverUrl(url);
        }
        productService.saveOrUpdate(product);
        return new JsonResult();
    }

    /**
     * 根据商品id查询商品详情内容
     */
    @RequestMapping("getContentById")
    @ResponseBody
    public ProductContent getContentById(Long id){
        return productService.getContentById(id);
    }
}
